package com.eiei.common;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Map.Entry;

import org.apache.log4j.Logger;

import com.eiei.utils.BlowFishUtils;

/**
 * 公共数据
 * @author eiei
 *
 */

public class CommonValue {

	public static Map<String,String> PROPERTIES_MAP ;
	
	public static Map<String,String> DB_MAP ;
	
	private static final Logger logger = Logger.getLogger(CommonValue.class); 
	
	private static String PASSWORD ;
	
	static{
		PROPERTIES_MAP = new HashMap<String,String>();
		DB_MAP = new HashMap<String,String>();
		InputStream inputStream = null ;
		Properties pros = new Properties();
        try {
        	inputStream = CommonValue.class.getClassLoader().getResourceAsStream("account.properties");
            pros.load(inputStream);
            for(Entry entry :pros.entrySet()){
            	PROPERTIES_MAP.put(entry.getKey().toString(), entry.getValue().toString());
            }
            inputStream.close();
            inputStream = CommonValue.class.getClassLoader().getResourceAsStream("db.properties");
            pros = new Properties();
            pros.load(inputStream);
            for(Entry entry :pros.entrySet()){
            	if(entry!=null && entry.getKey()!=null && entry.getValue()!=null){
            		DB_MAP.put(entry.getKey().toString(), entry.getValue().toString());
            	}
            }
        } catch (IOException ex) {
            logger.error(ex);
        } finally{
        	if(inputStream!=null){
        		try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        	}
        }
	}
	
	public static String getPassword() throws Exception{
		if(PASSWORD == null){
			String passwordMi = PROPERTIES_MAP.get("password");
			if(passwordMi == null || passwordMi.length()==0){
		         logger.error("配置文件没有设置密码");
		         throw new Exception();
			}
			PASSWORD = BlowFishUtils.decrypt(passwordMi);
		}
		return PASSWORD;
	}
	
	
	
	
	
	
}
